<?php
session_start();
$_SESSION['app_root'] = str_replace("//", "/", $_SERVER["DOCUMENT_ROOT"]."/softab/");
$_SESSION['app_uri'] = "//localhost/softab/";
require_once $_SESSION["app_root"]."DAL/adodb.inc.php";
require_once $_SESSION["app_root"]."DAL/adodb-exceptions.inc.php";
require_once $_SESSION["app_root"]."BLL/Enum.php";

define('APPDB_HOST', 'localhost');
define('APPDB_USER', 'root');
define('APPDB_PASS', '');
define('APPDB_DATABASE', 'appconfig');
define("APP_DB_DSN", "mysqli://root@localhost/appconfig");

define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_DATABASE', 'domainone');
define("DB_DSN", "mysqli://root@localhost/domainone_new");

function __autoload($class_name)
{
	$class_name;
	$class_type = "";
	if($class_type == ""){
		$class_type = strstr($class_name, "DAO") == "DAO"?"DAO":"";
	}
	if($class_type == ""){
		$class_type = strstr($class_name, "DVO") == "DVO"?"DVO":"";
	}
	if($class_type == ""){
		$class_type = strstr($class_name, "BLL") == "BLL"?"BLL":"";
	}
	if($class_type == ""){
		$class_type = strstr($class_name, "FieldDefaults") == "FieldDefaults"?"FieldDefaults":"";
	}
	switch($class_type){
		case "DAO":
			$path = $_SESSION['app_root']."DAO/".$class_name.".php";
			break;
		case "DVO":
			$path = $_SESSION['app_root']."DAO/DVO/".$class_name.".php";
			break;
		case "BLL":
			$path = $_SESSION['app_root']."BLL/".$class_name.".php";
			break;
		case "FieldDefaults":
			$path = $_SESSION['app_root']."BLL/FormFieldDefaults.php";
			break;
		default:
			$path = "error";
			break;
	}
	require_once $path;
}

function GetDatabase($class = null){
	$className = get_class($class);
	switch($className){
		case "User":
			$dsn = APP_DSN;
			break;
		default:
			$dsn = DB_DSN;
			break;
	}
	$db = ADONewConnection($dsn); # eg 'mysql' or 'postgres' 
	$db->debug = false;
	$db->SetFetchMode(ADODB_FETCH_ASSOC);
	return $db;
}

function vp($arr, $w = 0)
{
    echo "<pre>";
    if ($w == 1)
        var_dump($arr);
    else if ($w == 2)
        var_export($arr);
    else
        print_r($arr);
    echo "</pre>";
}

function alert($message)
{
    echo '	<script type="text/javascript" language="javascript">
				alert("'.$message.'");
			</script>';
}

function date2time($date)
{
    if ( isset ($date) && $date != "")
    {
        $data = explode('/', $date);
    $tmp = $data[2].'-'.$data[1].'-'.$data[0];
$time = strtotime($tmp);
}
else
{
    $time = time()+2000000000;
}
return $time;
}

/* BEGIN
 * UUID
 * @return uuid
 * @param object $serverID[optional]
 */
function uuid($serverID = 1)
{
$t = explode(" ", microtime());
return sprintf('%04x-%08s-%08s-%04s-%04x%04x',
				$serverID,
				clientIPToHex(),
				substr("00000000".dechex($t[1]), -8), // get 8HEX of unixtime
				substr("0000".dechex(round($t[0]*65536)), -4), // get 4HEX of microtime
				mt_rand(0, 0xffff), mt_rand(0, 0xffff));
}

function uuidDecode($uuid)
{
    $rez = Array();
$u = explode("-", $uuid);
if (is_array($u) && count($u) == 5)
{
    $rez = Array(
    'serverID'=>$u[0],
    'ip'=>clientIPFromHex($u[1]),
    'unixtime'=>hexdec($u[2]),
    'micro'=>(hexdec($u[3])/65536)
    );
}
return $rez;
}

function clientIPToHex($ip = "")
{
    $hex = "";
if ($ip == "")
    $ip = getEnv("REMOTE_ADDR");
$part = explode('.', $ip);
for ($i = 0; $i <= count($part)-1; $i++)
{
    $hex .= substr("0".dechex($part[$i]), -2);
}
return $hex;
}

function clientIPFromHex($hex)
{
    $ip = "";
if (strlen($hex) == 8)
{
    $ip .= hexdec(substr($hex, 0, 2)).".";
$ip .= hexdec(substr($hex, 2, 2)).".";
$ip .= hexdec(substr($hex, 4, 2)).".";
$ip .= hexdec(substr($hex, 6, 2));
}
return $ip;
}

/* END
 * UUID
 * @return
 * @param object $serverID[optional]
 */

function rmdir_rec($directory, $empty = FALSE)
{
    // if the path has a slash at the end we remove it here
    if (substr($directory, -1) == '/')
    {
        $directory = substr($directory, 0, -1);
}

// if the path is not valid or is not a directory ...
if (!file_exists($directory) || !is_dir($directory))
{
    // ... we return false and exit the function
    return FALSE;

// ... if the path is not readable
} elseif (!is_readable($directory))
{
    // ... we return false and exit the function
    return FALSE;

// ... else if the path is readable
} else
{

    // we open the directory
    $handle = opendir($directory);

// and scan through the items inside
while (FALSE !== ($item = readdir($handle)))
{
    // if the filepointer is not the current directory
    // or the parent directory
    if ($item != '.' && $item != '..')
    {
        // we build the new path to delete
        $path = $directory.'/'.$item;

    // if the new path is a directory
    if (is_dir($path))
    {
        // we call this function with the new path
        rmdir_rec($path);

    // if the new path is a file
} else
{
    // we remove the file
    unlink($path);
}
}
}
// close the directory
closedir($handle);

// if the option to empty is not set to true
if ($empty == FALSE)
{
    // try to delete the now empty directory
    if (!rmdir($directory))
    {
        // return false if not possible
        return FALSE;
}
}
// return success
return TRUE;
}
}
// ------------------------------------------------------------

?>
